home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / sparclit / salib.c < prev   
Encoding:
C/C++ Source or Header  |  1994-02-03  |  6.2 KB  |  371 lines

  1. /* Stand-alone library for SPARClite */
  2.  
  3. extern unsigned long get_xmt_status();
  4. extern void xmt_char();
  5.  
  6. asm("
  7.     .text
  8.     .align 4
  9.     .globl _get_uart_status
  10. _get_uart_status:
  11.     set 0x10000025, %o0
  12.     retl
  13.     lduba [%o0] 4, %o0
  14.  
  15.     .globl _set_uart_stuff
  16. _set_uart_stuff:
  17.     set 0x10000025, %o1
  18.     retl
  19.     stba %o0, [%o1] 4
  20.  
  21.     .globl _xmt_char
  22. _xmt_char:
  23.     set 0x10000021, %o1
  24.     retl
  25.     stba %o0, [%o1] 4
  26.  
  27.     .globl _rcv_char
  28. _rcv_char:
  29.     set 0x10000021, %o0
  30.     retl
  31.     lduba [%o0] 4, %o0
  32.  
  33.     .globl    _set_timer_3
  34. _set_timer_3:
  35.     set    0x10000078, %o1    ! Address of TCR3 reload register
  36.     retl
  37.     stha    %o0, [%o1] 4    ! Set the reg
  38.  
  39. ! Register window overflow handler.  Come here when save would move us
  40. ! into the invalid window.  This routine runs with traps disabled, and
  41. ! must be careful not to touch the condition codes, as PSR is never
  42. ! restored.
  43. !
  44. ! We are called with %l0 = wim, %l1 = pc, %l2 = npc
  45.  
  46.     .globl win_ovf
  47. win_ovf:
  48.     mov    %g1, %l3        ! Save g1, we use it to hold the wim
  49.     srl    %l0, 1, %g1        ! Rotate wim right
  50.     sll    %l0, 8-1, %l0
  51.     or    %l0, %g1, %g1
  52.  
  53.     save    %g0, %g0, %g0        ! Slip into next window
  54.     mov    %g1, %wim        ! Install the new wim
  55.  
  56.     std    %l0, [%sp + 0 * 4]    ! save L & I registers
  57.     std    %l2, [%sp + 2 * 4]
  58.     std    %l4, [%sp + 4 * 4]
  59.     std    %l6, [%sp + 6 * 4]
  60.  
  61.     std    %i0, [%sp + 8 * 4]
  62.     std    %i2, [%sp + 10 * 4]
  63.     std    %i4, [%sp + 12 * 4]
  64.     std    %i6, [%sp + 14 * 4]
  65.  
  66.     restore                ! Go back to trap window.
  67.     mov    %l3, %g1        ! Restore %g1
  68.  
  69.     jmpl    %l1,  %g0
  70.     rett    %l2
  71.  
  72. ! Register window underflow handler.  Come here when restore would move us
  73. ! into the invalid window.  This routine runs with traps disabled, and
  74. ! must be careful not to touch the condition codes, as PSR is never
  75. ! restored.
  76. !
  77. ! We are called with %l0 = wim, %l1 = pc, %l2 = npc
  78.  
  79.     .globl win_unf
  80. win_unf:
  81.     sll    %l0, 1, %l3        ! Rotate wim left
  82.     srl    %l0, 8-1, %l0
  83.     or    %l0, %l3, %l0
  84.  
  85.     mov    %l0, %wim        ! Install the new wim
  86.  
  87.     restore                ! User's window
  88.     restore                ! His caller's window
  89.  
  90.     ldd    [%sp + 0 * 4], %l0    ! restore L & I registers
  91.     ldd    [%sp + 2 * 4], %l2
  92.     ldd    [%sp + 4 * 4], %l4
  93.     ldd    [%sp + 6 * 4], %l6
  94.  
  95.     ldd    [%sp + 8 * 4], %i0
  96.     ldd    [%sp + 10 * 4], %i2
  97.     ldd    [%sp + 12 * 4], %i4
  98.     ldd    [%sp + 14 * 4], %i6
  99.  
  100.     save    %g0, %g0, %g0        ! Back to trap window
  101.     save    %g0, %g0, %g0
  102.  
  103.     jmpl    %l1,  %g0
  104.     rett    %l2
  105.  
  106. ! Turn on the cache
  107.  
  108.     .globl    _cache_on
  109. _cache_on:
  110.     sta    %g0, [%g0] 1        ! First, make sure cache is off
  111.                     !  before we diddle the tags
  112.     set    63 * 4 * 4, %o0
  113.     set    0x80000000, %o1
  114.  
  115. ! First, reset all of the cache line valid bits, then turn on the cache
  116.  
  117. cache_loop:
  118.     sta    %g0, [%o0] 0x0c        ! Bank 1, icache
  119.     sta    %g0, [%o0 + %o1] 0x0c    ! Bank 2, icache
  120.  
  121.     sta    %g0, [%o0] 0x0e        ! Bank 1, dcache
  122.     sta    %g0, [%o0 + %o1] 0x0e    ! Bank 2, dcache
  123.  
  124.     subcc    %o0, 16, %o0
  125.     bge    cache_loop
  126.     nop
  127.  
  128.     set    0x35, %o0        ! Write buf ena, Prefetch buf ena,
  129.                     ! Data & Inst caches enab
  130.     retl
  131.     sta    %o0, [%g0] 1        ! Turn on the cache
  132.  
  133. ! Flush the instruction cache.  We need to do this for the debugger stub so
  134. ! that breakpoints, et. al. become visible to the instruction stream after
  135. ! storing them in memory.
  136.  
  137.     .globl    _flush_i_cache
  138. _flush_i_cache:
  139.     lda    [%g0] 1, %o3        ! Get cache/bus interface reg
  140.     btst    1, %o3
  141.     bz    popj            ! Branch if i-cache is off
  142.     nop
  143.  
  144.     sta    %g0, [%g0] 1        ! Make sure cache is off
  145.     nop
  146.     nop
  147.     nop
  148.  
  149.     set    63 * 4 * 4, %o0        ! Loop size
  150.     set    0x80000000, %o1        ! Base addr of bank 2
  151. fi_loop:
  152.     lda    [%o0] 0x0c, %o2        ! Bank 1, icache
  153.     bclr    0x3c0, %o2        ! Clear valid bits
  154.     sta    %o2, [%o0] 0x0c
  155.  
  156.     lda    [%o0 + %o1] 0x0c, %o2    ! Bank 2, icache
  157.     bclr    0x3c0, %o2        ! Clear valid bits
  158.     sta    %o2, [%o0 + %o1] 0x0c
  159.  
  160.     subcc    %o0, 16, %o0
  161.     bge    fi_loop
  162.     nop
  163.  
  164.     sta    %o3, [%g0] 1        ! Turn cache back on
  165.  
  166. popj:    retl
  167.     nop
  168.  
  169.     .globl    _cache_off
  170.  
  171. _cache_off:
  172.     retl
  173.     nop
  174.  
  175. ! Read the TBR.
  176.  
  177.     .globl _rdtbr
  178. _rdtbr:
  179.     retl
  180.     mov    %tbr, %o0
  181.  
  182. ");
  183.  
  184. extern unsigned long rdtbr();
  185.  
  186. void
  187. __main()
  188. {
  189.   int x;
  190.  
  191. #if 0
  192.   set_uart_stuff(0x4e);
  193.   rdtbr();
  194.   rdtbr();
  195.   rdtbr();
  196.   rdtbr();
  197.   rdtbr();
  198.   set_uart_stuff(0x35);
  199. #endif
  200. };
  201.  
  202. /* Each entry in the trap vector occupies four words. */
  203.  
  204. struct trap_entry
  205. {
  206.   unsigned sethi_filler:10;
  207.   unsigned sethi_imm22:22;
  208.   unsigned jmpl_filler:19;
  209.   unsigned jmpl_simm13:13;
  210.   unsigned long filler[2];
  211. };
  212.  
  213. extern struct trap_entry fltr_proto;
  214. asm ("
  215.     .data
  216.     .globl _fltr_proto
  217.     .align 4
  218. _fltr_proto:            ! First level trap routine prototype
  219.     sethi 0, %l0
  220.     jmpl 0+%l0, %g0
  221.     nop
  222.     nop
  223.  
  224.     .text
  225.     .align 4
  226. ");
  227.  
  228. void
  229. exceptionHandler(tt, routine)
  230.      int tt;
  231.      unsigned long routine;
  232. {
  233.   struct trap_entry *tb;    /* Trap vector base address */
  234.  
  235.   tb = (struct trap_entry *)(rdtbr() & ~0xfff);
  236.  
  237.   tb[tt] = fltr_proto;
  238.  
  239.   tb[tt].sethi_imm22 = routine >> 10;
  240.   tb[tt].jmpl_simm13 = routine & 0x3ff;
  241. }
  242.  
  243. void
  244. update_leds()
  245. {
  246.   static unsigned char *leds = (unsigned char *)0x02000003;
  247.   static unsigned char curled = 1;
  248.   static unsigned char dir = 0;
  249.  
  250.   *leds = ~curled;
  251.  
  252.   if (dir)
  253.     curled <<= 1;
  254.   else
  255.     curled >>= 1;
  256.  
  257.   if (curled == 0)
  258.     {
  259.       if (dir)
  260.     curled = 0x80;
  261.       else
  262.     curled = 1;
  263.       dir = ~dir;
  264.     }
  265. }
  266.  
  267. #if 0
  268. void
  269. update_leds()
  270. {
  271.   static unsigned char *leds = (unsigned char *)0x02000003;
  272.   static unsigned char curled = 0xfe;
  273.  
  274.   *leds = curled;
  275.   curled = (curled >> 1) | (curled << 7);
  276. }
  277. #endif
  278.  
  279.  /* 1/5th of a second? */
  280.  
  281. #define LEDTIME (20000000 / 500)
  282.  
  283. int
  284. getDebugChar()
  285. {
  286.   unsigned long countdown = LEDTIME;
  287.  
  288.   update_leds();
  289.  
  290.   while (1)
  291.     {
  292.       if ((get_uart_status(0) & 2) != 0) break;
  293.  
  294.       if (countdown-- == 0)
  295.     {
  296.       countdown = LEDTIME;
  297.       update_leds();
  298.     }
  299.     }
  300.  
  301.   return rcv_char();
  302. }
  303.  
  304. /* Output one character to the serial port */
  305.  
  306. void
  307. putDebugChar(c)
  308.      int c;
  309. {
  310.   update_leds();
  311.  
  312.   while ((get_uart_status() & 1) == 0) ;
  313.  
  314.   xmt_char(c);
  315. }
  316.  
  317. int
  318. write(fd, data, length)
  319.      int fd;
  320.      unsigned char *data;
  321.      int length;
  322. {
  323.   int olength = length;
  324.  
  325.   while (length--)
  326.     putDebugChar(*data++);
  327.  
  328.   return olength;
  329. }
  330.  
  331. int
  332. read(fd, data, length)
  333.      int fd;
  334.      unsigned char *data;
  335.      int length;
  336. {
  337.   int olength = length;
  338.   int c;
  339.  
  340.   while (length--)
  341.     *data++ = getDebugChar();
  342.  
  343.   return olength;
  344. }
  345.  
  346. /* Set the baud rate for the serial port, returns 0 for success,
  347.    -1 otherwise */
  348.  
  349. int
  350. set_baud_rate(baudrate)
  351.      int baudrate;
  352. {
  353.   /* Convert baud rate to uart clock divider */
  354.   switch (baudrate)
  355.     {
  356.     case 38400:
  357.       baudrate = 16;
  358.       break;
  359.     case 19200:
  360.       baudrate = 33;
  361.       break;
  362.     case 9600:
  363.       baudrate = 65;
  364.       break;
  365.     default:
  366.       return -1;
  367.     }
  368.  
  369.   set_timer_3(baudrate);    /* Set it */
  370. }
  371.